package com.demo.camera2;

import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;

import com.caiguoping.yuv.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class EasyCamera implements Camera.PreviewCallback {

    private Camera camera;

    private byte[] buff;

    private SurfaceTexture texture;

    private PreviewCallback previewCallback;

    public void setPreviewCallback(PreviewCallback previewCallback) {
        this.previewCallback = previewCallback;
    }

    public void setTexture(SurfaceTexture texture) {
        this.texture = texture;
    }

    public int open(String productName, int width, int height) {
        int cameraIndex = -1;

        File[] videoFiles = new File("/sys/class/video4linux")
                .listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.getName().startsWith("video");
                    }
                });
        assert videoFiles != null;
        for (File videoFile : videoFiles) {
            InputStreamReader inputStreamReader = null;
            BufferedReader bufferedReader = null;
            try {
                inputStreamReader = new InputStreamReader(new FileInputStream(new File(videoFile, "name")));
                bufferedReader = new BufferedReader(inputStreamReader);
                String name = bufferedReader.readLine();
                if (productName.equals(name)) {
                    String index = videoFile.getName().substring(5);
                    cameraIndex = Integer.parseInt(index);
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (inputStreamReader != null) {
                        inputStreamReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (cameraIndex != -1) {
            try {
                camera = Camera.open(cameraIndex);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(width, height);
                parameters.setPreviewFormat(ImageFormat.NV21);
                camera.setParameters(parameters);
                camera.setPreviewCallbackWithBuffer(this);
                camera.setPreviewTexture(texture);
                camera.startPreview();

                buff = Test.createByteBuff(width, height, Test.FORMAT_NV21);
                camera.addCallbackBuffer(buff);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return cameraIndex;
    }

    public void close() {
        if (camera != null) {
            camera.stopPreview();
            camera.setPreviewCallbackWithBuffer(null);
            camera.release();
        }
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if (previewCallback != null) {
            Camera.Parameters parameters = camera.getParameters();
            Camera.Size size = parameters.getPreviewSize();

            previewCallback.onPreviewFrame(data, size.width, size.height);
        }
        camera.addCallbackBuffer(buff);
    }
}
